import { Injectable, Inject } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';

import { Observable } from 'rxjs/Observable';

import * as moment from 'moment';
import { List } from '../models/list';

// import { User } from '../models/index';
// import { Header } from 'primeng/primeng';

@Injectable()
export class ListService {

    //  url1 = "http://203.81.246.122:8080/api/security/login";


    constructor(private http: HttpClient, @Inject('API_URL') private url: string) { }


    getCountHistory(typeId, deviceId, count): Observable<any> {

        const url = `${this.url}${typeId}/essential/latest/target/${deviceId}/${count}`;
        const headers = new HttpHeaders()
            .set('Accept', 'application/json;charset=UTF-8');

        return this.http
            .get(url, { headers: headers })
    }


    getTimeHistory(typeId, deviceId, start, end, length): Observable<any> {

        const url = `${this.url}${typeId}/essential/target/${deviceId}/${start}/${end}?offset=0&length=${length}`;
        console.log(url)
        const headers = new HttpHeaders()
            .set('Accept', 'application/json;charset=UTF-8');

        return this.http
            .get(url, { headers: headers })
    }


    count(typeId, targetId, start, end) {
        const url = `${this.url}${typeId}/essential/count/target/${targetId}/${start}/${end}`;
        const headers = new HttpHeaders()
            .set('Accept', 'application/json;charset=UTF-8');
        return this.http
            .get(url, { headers: headers })
    }



    queryAllData(typeId, id,start, end, offset: number, length: number): Observable<any[]> {
        const url = `${this.url}${typeId}/essential/target/${id}/${start}/${end}?offset=${offset}&length=${length}`;
        console.log(url);
        return this.http
            .get<any[]>(url)
            .map(data => {                      // 返回 数据
                const warns: List[] = [];          //  定义空数组
                data.forEach(item => {         // 对请求数据进行遍历
                    warns.push(                   // 将格式化的数据放到新数组中
                        Object.assign(item, {
                            //  YYYYMMDDHHmmss格式转换为YYYY-MM-DD hh:mm:ss  格式化
                            time: moment(item.time, 'YYYYMMDDHHmmss').format('YYYY-MM-DD HH:mm:ss')
                        }));
                });
                return warns;
            });
    }


}
